cmake_minimum_required(VERSION 3.31)
project(PingTools)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH "D:/Qt/Qt5.14.2/5.14.2/mingw73_64")


# 新增资源文件安装
install(DIRECTORY resources/ DESTINATION resources)

# 新增测试配置
enable_testing()
add_subdirectory(tests)

# 修改输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# 设置图标资源
set(APP_ICON_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/src/resources/iocn.ico")
set(RC_FILE "${CMAKE_CURRENT_SOURCE_DIR}/src/resources/app_icon.rc")  # 新增RC文件路径

# 同时复制ICO和RC文件到构建目录
file(COPY ${APP_ICON_WINDOWS} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${RC_FILE} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})  # 新增RC文件复制


find_package(Qt5 COMPONENTS
        Core
        Gui
        Widgets
        REQUIRED)

add_executable(PingTools WIN32
        src/ui/home.cpp
        src/ui/home.h
        src/ui/home.ui
        src/core/PingTools.cpp
        src/core/PingTools.h
        src/app/AppClient.cpp
        src/app/AppClient.h
        src/main.cpp
        ${CMAKE_CURRENT_BINARY_DIR}/app_icon.rc
        resources/logo.png
        src/resources/iocn.ico
        src/resources/resources.qrc
)


target_link_libraries(PingTools
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
)


if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()